﻿// CalibrateView.cpp: 实现文件
//

#include "stdafx.h"
#include "DC-QC.h"
#include "CalibrateView.h"
#include "math.h"
#include "CChromParsDlg.h"
#include "BaseLineCaliDlg.h"

#define  CALIBRATEVIEW_BTAREA_HEIGHT		360
#define  CALIBRATEVIEW_BT_WIDTH		70

// CCalibrateView

IMPLEMENT_DYNCREATE(CCalibrateView, CFormView)

CCalibrateView::CCalibrateView()
	: CFormView(IDD_CCalibrateView)
{
	m_nCurIndex = 0;
	m_nCurPartIndex = 0;
	m_nOldCount = 0;
	m_nCurID = -1;
	m_nCh=0;
	m_bUpdateFormView=FALSE;
}

CCalibrateView::~CCalibrateView()
{
}

void CCalibrateView::DoDataExchange(CDataExchange* pDX)
{
	CFormView::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_BTUP, m_btUp);
	DDX_Control(pDX, IDC_BTDOWN, m_btDown);
	DDX_Control(pDX, IDC_BTOPENFILE, m_btOpen);
	DDX_Control(pDX, IDC_BTKEYBOARD, m_btKeyboard);
	DDX_Text(pDX, IDC_C1, m_dC[0]);
	DDX_Text(pDX, IDC_C2, m_dC[1]);
	DDX_Text(pDX, IDC_C3, m_dC[2]);
	DDX_Text(pDX, IDC_C4, m_dC[3]);
	DDX_Text(pDX, IDC_C5, m_dC[4]);
	DDX_Text(pDX, IDC_C6, m_dC[5]);
	DDX_Text(pDX, IDC_C7, m_dC[6]);
	DDX_Text(pDX, IDC_C8, m_dC[7]);
	DDX_Text(pDX, IDC_C9, m_dC[8]);
	DDX_Text(pDX, IDC_C10, m_dC[9]);
	DDX_Text(pDX, IDC_CA1, m_dArea[0]);
	DDX_Text(pDX, IDC_CA2, m_dArea[1]);
	DDX_Text(pDX, IDC_CA3, m_dArea[2]);
	DDX_Text(pDX, IDC_CA4, m_dArea[3]);
	DDX_Text(pDX, IDC_CA5, m_dArea[4]);
	DDX_Text(pDX, IDC_CA6, m_dArea[5]);
	DDX_Text(pDX, IDC_CA7, m_dArea[6]);
	DDX_Text(pDX, IDC_CA8, m_dArea[7]);
	DDX_Text(pDX, IDC_CA9, m_dArea[8]);
	DDX_Text(pDX, IDC_CA10, m_dArea[9]);
	DDX_Text(pDX, IDC_SH1, m_nS[0]);
	DDX_Text(pDX, IDC_SH2, m_nS[1]);
	DDX_Text(pDX, IDC_SH3, m_nS[2]);
	DDX_Text(pDX, IDC_SH4, m_nS[3]);
	DDX_Text(pDX, IDC_SH5, m_nS[4]);
	DDX_Text(pDX, IDC_SH6, m_nS[5]);
	DDX_Text(pDX, IDC_SH7, m_nS[6]);
	DDX_Text(pDX, IDC_SH8, m_nS[7]);
	DDX_Text(pDX, IDC_SH9, m_nS[8]);
	DDX_Text(pDX, IDC_SH10, m_nS[9]);
}

BEGIN_MESSAGE_MAP(CCalibrateView, CFormView)
	ON_WM_PAINT()
	ON_WM_CREATE()
	ON_WM_SIZE()
	ON_WM_CTLCOLOR()
	ON_BN_CLICKED(IDC_BTTWOCHCALI, &CCalibrateView::OnBnClickedBttwochcali)
	ON_BN_CLICKED(IDC_BTSORT, &CCalibrateView::OnBnClickedBtsort)
	ON_BN_CLICKED(IDC_BTPRE, &CCalibrateView::OnBnClickedBtpre)
	ON_BN_CLICKED(IDC_BTNEXT, &CCalibrateView::OnBnClickedBtnext)
	ON_BN_CLICKED(IDC_BTSAVE, &CCalibrateView::OnBnClickedBtsave)
	ON_BN_CLICKED(IDC_BTLOAD, &CCalibrateView::OnBnClickedBtload)
	ON_BN_CLICKED(IDC_BTSAVEAS, &CCalibrateView::OnBnClickedBtsaveas)
	ON_BN_CLICKED(IDC_BTEXPORT, &CCalibrateView::OnBnClickedBtexport)
	ON_BN_CLICKED(IDC_BTINTPAR, &CCalibrateView::OnBnClickedBtintpar)
	ON_BN_CLICKED(IDC_BTUP, &CCalibrateView::OnBnClickedBtup)
	ON_BN_CLICKED(IDC_BTDOWN, &CCalibrateView::OnBnClickedBtdown)
	ON_WM_LBUTTONUP()
	ON_WM_TIMER()
	ON_BN_CLICKED(IDC_BTAPPLY, &CCalibrateView::OnBnClickedBtapply)
	ON_BN_CLICKED(IDC_BTOPENFILE, &CCalibrateView::OnBnClickedBtopenfile)
	ON_BN_CLICKED(IDC_BTKEYBOARD, &CCalibrateView::OnBnClickedBtkeyboard)
	ON_BN_CLICKED(IDC_BTINTPAR2, &CCalibrateView::OnBnClickedBtintpar2)
	ON_BN_CLICKED(IDC_BTCHA, &CCalibrateView::OnBnClickedBtcha)
	ON_BN_CLICKED(IDC_BTCHB, &CCalibrateView::OnBnClickedBtchb)
END_MESSAGE_MAP()


// CCalibrateView 诊断

#ifdef _DEBUG
void CCalibrateView::AssertValid() const
{
	CFormView::AssertValid();
}

#ifndef _WIN32_WCE
void CCalibrateView::Dump(CDumpContext& dc) const
{
	CFormView::Dump(dc);
}
#endif
#endif //_DEBUG


// CCalibrateView 消息处理程序


BOOL CCalibrateView::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext)
{
	// TODO: 在此添加专用代码和/或调用基类

	return CFormView::Create(lpszClassName, lpszWindowName, dwStyle, rect, pParentWnd, nID, pContext);
}


void CCalibrateView::OnInitialUpdate()
{
	CFormView::OnInitialUpdate();

	// TODO: 在此添加专用代码和/或调用基类
	CWnd *pWnd = GetTopWindow();
	while (NULL != pWnd)
	{
		pWnd->SetFont(&gl.m_pSmallFont);
		pWnd = pWnd->GetNextWindow();
	}
	GetData();
	((CButton*)GetDlgItem(IDC_CHNAME))->SetWindowText(_T("通道A"));

	m_nTimer = SetTimer(9006,1000, NULL);
	m_btKeyboard.SetIcon(IDI_KEYBOARD, NULL);
}

void CCalibrateView::OnPaint()
{
	CPaintDC dc(this); // device context for painting
					   // TODO: 在此处添加消息处理程序代码
					   // 不为绘图消息调用 CFormView::OnPaint()
	CRect rect;
	GetClientRect(&rect);

	CBitmap ClientScreen, *pOldBitmap;
	CRect UpdateRect(rect);
	CDC MDC;
	MDC.CreateCompatibleDC(&dc);
	ClientScreen.CreateCompatibleBitmap(&dc, rect.Width(), rect.Height());
	pOldBitmap = MDC.SelectObject(&ClientScreen);
	MDC.FillSolidRect(UpdateRect, RGB(255, 255, 255));
	Draw(&MDC, rect);
	dc.BitBlt(UpdateRect.left, UpdateRect.top,
		UpdateRect.right - UpdateRect.left,
		UpdateRect.bottom - UpdateRect.top,
		&MDC, UpdateRect.left, UpdateRect.top, SRCCOPY);
	MDC.SelectObject(pOldBitmap);

	ClientScreen.DeleteObject();
	MDC.DeleteDC();
}

void CCalibrateView::Draw(CDC* pDC, CRect rt)
{
	try
	{
		DrawInBorder(pDC, rt);
		rt.DeflateRect(1, 1, 1, 1);
		//BxGradientFill(pDC, rt, BACKCOLOR1, BACKCOLOR2, 1);
		pDC->FillSolidRect(rt, BACKCOLOR2);

		DrawInBorder(pDC, m_lstArea);
		BxGradientFill(pDC, m_lstArea, BACKCOLOR1, BACKCOLOR2, 1);
		DrawChromList(pDC, m_lstArea);

		GetDlgItem(IDC_GRAPHAREA)->GetWindowRect(rt);
		ScreenToClient(rt);
		rt.DeflateRect(1, 1, 1, 1);
		DrawInBorder(pDC, rt);
		rt.DeflateRect(1, 1, 1, 1);
		pDC->FillSolidRect(rt, RGB(146,177,166));
		DrawCaliFrame(pDC, rt);
		DrawCaliCurve(pDC, rt);
	}
	catch (...)
	{
	}
}

void	CCalibrateView::DrawCaliFrame(CDC* pDC, CRect rtArea)
{
	int OX = rtArea.left + 50;
	int OY = rtArea.top + 10;
	CPen pen(0, 1, RGB(240, 240, 240));
	CPen pen1(0, 1, RGB(180, 180, 180));
	CPen*   pOldPen = pDC->SelectObject(&pen);
	LOGFONT lf;								//LOGFONT结构中的变量用于定义字体的各种特性
	memset(&lf, 0, sizeof(lf));
	lf.lfCharSet = CHINESEBIG5_CHARSET;		//设置字符集
	lf.lfHeight = 14;
	wcscpy(lf.lfFaceName, _T("宋体"));		//设置字体名称 MingLiu为MS提供的BIG5字体
	CFont font;
	font.CreateFontIndirect(&lf);			//创建字体
	CFont* pF = (CFont*)pDC->SelectObject(&font);	//保存当前字体
	double dXSpace = (rtArea.Width() - 70) / 6.0;
	double dYSpace = (rtArea.Height() - 30) / 6.0;
	m_nWidth = rtArea.Width() - 70;
	m_nHeight = rtArea.Height() - 30;
	for (int i = 0; i <= 6; i++)
	{
		pDC->SelectObject(&pen);
		pDC->MoveTo(OX + i * dXSpace, OY);
		pDC->LineTo(OX + i * dXSpace, OY + dYSpace * 6);
		pDC->MoveTo(OX, OY + i * dYSpace);
		pDC->LineTo(OX + dXSpace * 6, OY + i * dYSpace);
		CString str;
		switch (i)
		{
		case 0:
			str = _T("0.0001");
			break;
		case 1:
			str = _T("0.001");
			break;
		case 2:
			str = _T("0.01");
			break;
		case 3:
			str = _T("0.1");
			break;
		case 4:
			str = _T("1");
			break;
		case 5:
			str = _T("10");
			break;
		case 6:
			str = _T("100");
			break;
		}
		pDC->TextOut(OX + i * dXSpace - 8, OY + dYSpace * 6 + 6, str);

		switch (i)
		{
		case 0:
			str = _T("0.001");
			break;
		case 1:
			str = _T("0.01");
			break;
		case 2:
			str = _T("0.1");
			break;
		case 3:
			str = _T("1");
			break;
		case 4:
			str = _T("10");
			break;
		case 5:
			str = _T("100");
			break;
		case 6:
			str = _T("1000");
			break;
		}
		pDC->TextOut(OX - 35, OY + dYSpace * 6 - i * dYSpace - 5, str);

		if (i != 6)
		{
			pDC->SelectObject(&pen1);
			for (int k = 2; k <= 9; k++)
			{
				pDC->MoveTo(OX + i * dXSpace + dXSpace * log10(k*1.0), OY);
				pDC->LineTo(OX + i * dXSpace + dXSpace * log10(k*1.0), OY + dYSpace * 6);
				pDC->MoveTo(OX, OY + (i + 1)*dYSpace - dYSpace * log10(k*1.0));
				pDC->LineTo(OX + dXSpace * 6, OY + (i + 1)*dYSpace - dYSpace * log10(k*1.0));
			}
		}
	}
	pDC->SelectObject(pOldPen);
	pDC->SetTextColor(RGB(255, 0, 0));
	if (m_nCurIndex < 7)
		pDC->TextOut(OX + 10, OY, _T("峰面积"));
	else
		pDC->TextOut(OX + 10, OY, _T("信号"));

	pDC->TextOut(OX + dXSpace * 6 - 10, OY + dYSpace * 6 - 15, _T("浓度"));
	pDC->MoveTo(OX, OY);
	pDC->LineTo(OX, OY + dYSpace * 6);
	pDC->MoveTo(OX, OY + dYSpace * 6);
	pDC->LineTo(OX + dXSpace * 6, OY + dYSpace * 6);

	CString strTemp;
	pDC->SetTextColor(RGB(64, 0, 128));
	CString strHA;
	if (gl.gcMachine.nIntMethod == 0)
		strHA=_T("峰面积");
	else
		strHA = _T("峰高");

	if(m_dB>=0)
		strTemp.Format(_T("浓度=%.4f*%s+%.4f  相关系数=%.4f"), m_dK, strHA, m_dB, m_dR);
	else
		strTemp.Format(_T("浓度=%.4f*%s%.4f  相关系数=%.4f"), m_dK, strHA, m_dB, m_dR);

	pDC->TextOut(OX + 60, OY + 20, strTemp);

	pDC->SelectObject(&pF);
	pF->DeleteObject();

	m_ptOrign = CPoint(OX, OY + dYSpace * 6);
}
void	CCalibrateView::DrawCaliCurve(CDC* pDC, CRect rtArea)
{
	CPen pen(0, 2, RGB(15, 15, 245));
	CPen*   pOldPen = pDC->SelectObject(&pen);
	int x;
	int y;
	CPoint point(m_ptOrign);
	bool bFirstPoint = TRUE;
	for (int i = 0; i < 10; i++)
	{
		if (m_dC[i] != 0 || m_dArea[i] != 0)
		{
			if (m_nCurIndex == 7)
			{
				x = m_ptOrign.x + m_nWidth * (log10(m_dC[i]) + 4) / 6;
				y = m_ptOrign.y - m_nHeight * (log10(m_dArea[i] / 1000.0) + 3) / 6;
			}
			else
			{
				x = m_ptOrign.x + m_nWidth * (log10(m_dC[i]) + 4) / 6;
				y = m_ptOrign.y - m_nHeight * (log10(m_dArea[i]) + 3) / 6;
			}
			point.x = x;
			point.y = y;
			if (bFirstPoint)
			{
				pDC->MoveTo(point);
				bFirstPoint = FALSE;
			}
			else
				pDC->LineTo(point);
			CRect rt = CRect(point, point);
			rt.InflateRect(2, 2);
			pDC->Ellipse(rt);
			pDC->MoveTo(point);
		}
	}
	pDC->SelectObject(pOldPen);
}

int CCalibrateView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CFormView::OnCreate(lpCreateStruct) == -1)
		return -1;

	// TODO:  在此添加您专用的创建代码
	CRect rect(0, 0, 10, 10);
	m_wndCurve.Create(NULL, NULL, WS_CHILD | WS_VISIBLE, rect, this, WND_CALICHROMCURVE_ID, NULL);
	m_wndIntResult.Create(NULL, NULL, WS_CHILD | WS_VISIBLE, rect, this, WND_CALIINTRESULT_ID, NULL);

	return 0;
}

void	CCalibrateView::RelayOut()
{
	if (gl.gcMachine.nIntMethod==0)
		GetDlgItem(IDC_U1)->SetWindowTextW(_T("峰面积\n（mVS）"));
	else
		GetDlgItem(IDC_U1)->SetWindowTextW(_T("峰高\n（mV）"));

	CRect rect;
	GetClientRect(rect);
	try
	{
		if (m_btUp.m_hWnd)
		{
			CRect rt = rect;
			rt.top = rect.top + CALIBRATEVIEW_BTAREA_HEIGHT*g_DPIScaleY;
			rt.right = rect.left + rect.Width() /2-20*g_DPIScaleX;
			m_wndCurve.MoveWindow(rt);

			rt.left = rt.right + CALIBRATEVIEW_BT_WIDTH*g_DPIScaleX + 5*g_DPIScaleX;
			rt.right = rect.right;
			m_wndIntResult.MoveWindow(rt);

			rt.left = rect.left + rect.Width() / 2 -15*g_DPIScaleX;
			rt.right = rt.left + CALIBRATEVIEW_BT_WIDTH*g_DPIScaleX;
			rt.bottom = rt.top + 32*g_DPIScaleY;
			m_btUp.MoveWindow(rt);
			m_lstArea = rt;
			m_lstArea.top = rt.bottom + 5*g_DPIScaleY;

			rt.bottom = rect.bottom - 32*g_DPIScaleY;
			rt.top = rect.bottom - 60*g_DPIScaleY;
			m_btDown.MoveWindow(rt);
			m_lstArea.bottom = rt.top - 5*g_DPIScaleY;
			rt.bottom = rect.bottom - 2*g_DPIScaleX;
			rt.top = rect.bottom - 30*g_DPIScaleY;
			m_btOpen.MoveWindow(rt);

			GetDlgItem(IDC_GRAPHAREA)->GetWindowRect(rt);
			ScreenToClient(rt);
			rt.right = rect.right - 5*g_DPIScaleX;
			GetDlgItem(IDC_GRAPHAREA)->MoveWindow(rt);

			Invalidate();
		}
	}
	catch (...)
	{
	}

}

void CCalibrateView::OnSize(UINT nType, int cx, int cy)
{
	CFormView::OnSize(nType, cx, cy);

	// TODO: 在此处添加消息处理程序代码
	CRect rect;
	GetClientRect(rect);
	try
	{
		if (m_btUp.m_hWnd)
		{
			CRect rt = rect;
			rt.top = rect.top + CALIBRATEVIEW_BTAREA_HEIGHT*g_DPIScaleY;
			rt.right = rect.left + rect.Width() / 2-20*g_DPIScaleX;
			m_wndCurve.MoveWindow(rt);

			rt.left = rt.right + CALIBRATEVIEW_BT_WIDTH*g_DPIScaleX + 5*g_DPIScaleX;
			rt.right = rect.right;
			m_wndIntResult.MoveWindow(rt);

			rt.left = rect.left + rect.Width() / 2 -15*g_DPIScaleX;
			rt.right = rt.left + CALIBRATEVIEW_BT_WIDTH*g_DPIScaleX;
			rt.bottom = rt.top + 32*g_DPIScaleY;
			m_btUp.MoveWindow(rt);
			m_lstArea = rt;
			m_lstArea.top = rt.bottom + 5*g_DPIScaleY;

			rt.bottom = rect.bottom - 32*g_DPIScaleY;
			rt.top = rect.bottom - 60*g_DPIScaleY;
			m_btDown.MoveWindow(rt);
			m_lstArea.bottom = rt.top - 5*g_DPIScaleY;
			rt.bottom = rect.bottom - 2*g_DPIScaleY;
			rt.top = rect.bottom - 30*g_DPIScaleY;
			m_btOpen.MoveWindow(rt);

			GetDlgItem(IDC_GRAPHAREA)->GetWindowRect(rt);
			ScreenToClient(rt);
			rt.right = rect.right - 5*g_DPIScaleX;
			GetDlgItem(IDC_GRAPHAREA)->MoveWindow(rt);
		}
	}
	catch (...)
	{
	}
}


HBRUSH CCalibrateView::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
	HBRUSH hbr = CFormView::OnCtlColor(pDC, pWnd, nCtlColor);

	// TODO:  在此更改 DC 的任何特性
	if (nCtlColor == CTLCOLOR_STATIC)
	{
		switch (pWnd->GetDlgCtrlID())
		{
		case IDC_CHNAME:
			pDC->SetTextColor(RGB(255, 0, 0));
			pDC->SetBkColor(RGB(55, 210, 210));
			return gl.m_brBack2;
			break;
		}
		pDC->SetBkMode(TRANSPARENT);
		return gl.m_brBack1;
	}

	// TODO:  如果默认的不是所需画笔，则返回另一个画笔
	return hbr;
}

//组份\n双通道校准
void CCalibrateView::OnBnClickedBttwochcali()
{
	// TODO: 在此添加控件通知处理程序代码
	//CTwoChCaliDlg dlg;
	CBaseLineCaliDlg dlg;
	dlg.DoModal();
}

void CCalibrateView::GetData()
{
	m_bModify = FALSE;
	int nNums = 0;
	float dC[10], dA[10];
	if(m_nCh==0)
	{
		for (int i = 0; i < 10; i++)
		{
			m_dC[i] = gl.gcCi.scA[m_nCurIndex].PPM[i];
			m_dArea[i] = gl.gcCi.scA[m_nCurIndex].Area[i];
			//处理一下小数位
			m_dC[i] = (int)(m_dC[i] * 10000) / 10000.0;
			m_dArea[i] = (int)(m_dArea[i] * 10000) / 10000.0;

			m_nS[i] = gl.gcCi.scA[m_nCurIndex].nStandardGas[i];
			if (i != 0 && m_dArea[i] != 0)
			{
				dC[nNums] = m_dC[i];
				dA[nNums] = m_dArea[i];
				nNums++;
			}
		}
		GetDlgItem(IDC_NAME)->SetWindowText(gl.gcCi.scA[m_nCurIndex].strName);
	}
	else
	{
		for (int i = 0; i < 10; i++)
		{
			m_dC[i] = gl.gcCi.scB[m_nCurIndex].PPM[i];
			m_dArea[i] = gl.gcCi.scB[m_nCurIndex].Area[i];
			//处理一下小数位
			m_dC[i] = (int)(m_dC[i] * 10000) / 10000.0;
			m_dArea[i] = (int)(m_dArea[i] * 10000) / 10000.0;

			m_nS[i] = gl.gcCi.scB[m_nCurIndex].nStandardGas[i];
			if (i != 0 && m_dArea[i] != 0)
			{
				dC[nNums] = m_dC[i];
				dA[nNums] = m_dArea[i];
				nNums++;
			}
		}
		GetDlgItem(IDC_NAME)->SetWindowText(gl.gcCi.scB[m_nCurIndex].strName);
	}
	gl.CaculateK(nNums, dC, dA, m_dK, m_dB, m_dR);

	if (m_nCurIndex < 7)
		GetDlgItem(IDC_U1)->SetWindowText(_T("峰面积\n(mVS)"));
	else if (m_nCurIndex == 7)
		GetDlgItem(IDC_U1)->SetWindowText(_T("信号\n(mV)"));
	else if (m_nCurIndex == 8)
		GetDlgItem(IDC_U1)->SetWindowText(_T("信号\n(mV)"));
	else if (m_nCurIndex == 9)
		GetDlgItem(IDC_U1)->SetWindowText(_T("信号\n(mV)"));
	UpdateData(FALSE);
	Invalidate(FALSE);
}

void CCalibrateView::SetData()
{
	m_bModify = TRUE;
	UpdateData(TRUE);
	if(m_nCh==0)
	{
		for (int i = 0; i < 10; i++)
		{
			gl.gcCi.scA[m_nCurIndex].PPM[i] = m_dC[i];
			gl.gcCi.scA[m_nCurIndex].Area[i] = m_dArea[i];
			if (gl.gcMachine.nIntMethod == 0)
				gl.gcCi.scA[m_nCurIndex].OrgArea[i] = m_dArea[i];
			else
				gl.gcCi.scA[m_nCurIndex].OrgHeight[i] = m_dArea[i];
			gl.gcCi.scA[m_nCurIndex].nStandardGas[i]= m_nS[i];
		}
	}
	else
	{
		for (int i = 0; i < 10; i++)
		{
			gl.gcCi.scB[m_nCurIndex].PPM[i] = m_dC[i];
			gl.gcCi.scB[m_nCurIndex].Area[i] = m_dArea[i];
			if (gl.gcMachine.nIntMethod == 0)
				gl.gcCi.scB[m_nCurIndex].OrgArea[i] = m_dArea[i];
			else
				gl.gcCi.scB[m_nCurIndex].OrgHeight[i] = m_dArea[i];
			gl.gcCi.scB[m_nCurIndex].nStandardGas[i]= m_nS[i];
		}
	}
}

void CCalibrateView::OnBnClickedBtsort()
{
	UpdateData(TRUE);

	for (int i = 0; i < 9; i++)
	{
		double dMin = m_dC[i];
		int		nMinIndex = i;
		for (int k = i + 1; k < 10; k++)
		{
			if (dMin > m_dC[k])
			{
				dMin = m_dC[k];
				nMinIndex = k;
			}
		}
		if (nMinIndex != i)
		{
			dMin = m_dC[i];
			m_dC[i] = m_dC[nMinIndex];
			m_dC[nMinIndex] = dMin;
			dMin = m_dArea[i];
			m_dArea[i] = m_dArea[nMinIndex];
			m_dArea[nMinIndex] = dMin;
		}
	}
	//处理一下0，0的问题
	int nIndex = 0;
	for (int i = 0; i < 10; i++)
	{
		if (m_dC[i] == 0)
			nIndex = i;
		else
			break;
	}
	//移位
	for (int i = nIndex; i < 10; i++)
	{
		double tt = m_dC[i];
		m_dC[i - nIndex] = m_dC[i];
		tt = m_dArea[i];
		m_dArea[i - nIndex] = m_dArea[i];
	}
	for (int i = 0; i < nIndex; i++)
	{
		m_dC[9 - i] = 0;
		m_dArea[9 - i] = 0;
	}

	UpdateData(FALSE);
}

void CCalibrateView::OnBnClickedBtapply()
{
	// TODO: 在此添加控件通知处理程序代码
	SetData();
	Invalidate(FALSE);
}


void CCalibrateView::OnBnClickedBtpre()
{
	if (m_nCurIndex > 0)
	{
		m_nCurIndex--;
		GetData();
	}
}

void CCalibrateView::OnBnClickedBtnext()
{
	if(	m_nCh==0)
	{
		if (m_nCurIndex < 8)
		{
			m_nCurIndex++;
			GetData();
		}
	}
	else
	{
		if (m_nCurIndex < 6)
		{
			m_nCurIndex++;
			GetData();
		}
	}
}

void CCalibrateView::OnBnClickedBtsave()
{
	// TODO: 在此添加控件通知处理程序代码
	SetData();
	Invalidate(FALSE);

	CString strFileName;
	strFileName.Format(_T("%s\\SystemMethod.mtd"), m_strAppPath);
	gl.SaveCalMethod(strFileName);
}

//加载
void CCalibrateView::OnBnClickedBtload()
{
	// TODO: 在此添加控件通知处理程序代码
	CString strFile;
	CFileDialog dlg(true, TEXT("mtd"), NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, _T("色谱方法文件(*.mtd)|*.mtd||"));
	if (dlg.DoModal() != IDOK) return;
	strFile = dlg.GetPathName();
	gl.LoadCalMethod(strFile);
	GetData();
}

//另存为
void CCalibrateView::OnBnClickedBtsaveas()
{
	// TODO: 在此添加控件通知处理程序代码
	CString strFile;
	CFileDialog dlg(FALSE, TEXT("mtd"), NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, _T("色谱方法文件(*.mtd)|*.mtd||"));
	if (dlg.DoModal() != IDOK) return;
	strFile = dlg.GetPathName();
	gl.SaveCalMethod(strFile);
}

//输出--打开另一个界面，出设备标定报告
void CCalibrateView::OnBnClickedBtexport()
{
	// TODO: 在此添加控件通知处理程序代码
	AfxMessageBox(_T("色谱标定报告输出！--功能开发过程中"));
}

//通道A峰参数
void CCalibrateView::OnBnClickedBtintpar()
{
	gl.StartNumberKeyBoard();
	CChromParsDlg dlg;
	dlg.m_nType=0;
	dlg.DoModal();
	gl.CloseNumberKeyBoard();
	m_nCh=0;
	m_nCurIndex=0;
	((CButton*)GetDlgItem(IDC_CHNAME))->SetWindowText(_T("通道A"));
	GetData();
}
//通道B峰参数
void CCalibrateView::OnBnClickedBtintpar2()
{
	gl.StartNumberKeyBoard();
	CChromParsDlg dlg;
	dlg.m_nType=1;
	dlg.DoModal();
	gl.CloseNumberKeyBoard();
	m_nCh=1;
	m_nCurIndex=0;
	((CButton*)GetDlgItem(IDC_CHNAME))->SetWindowText(_T("通道B"));
	GetData();
}

void CCalibrateView::OnBnClickedBtcha()
{
	// TODO: 在此添加控件通知处理程序代码
	m_nCh=0;
	m_nCurIndex=0;
	((CButton*)GetDlgItem(IDC_CHNAME))->SetWindowText(_T("通道A"));
	GetData();
}

void CCalibrateView::OnBnClickedBtchb()
{
	// TODO: 在此添加控件通知处理程序代码
	m_nCh=1;
	m_nCurIndex=0;
	((CButton*)GetDlgItem(IDC_CHNAME))->SetWindowText(_T("通道B"));
	GetData();
}

//列表刷新
void	CCalibrateView::RefreshList()
{
	int nCount = gl.m_lstChromFiles.GetCount();
	//显示最新的10个
	m_nCurPartIndex = nCount-10;
	if (m_nCurPartIndex < 0) m_nCurPartIndex = 0;
	int nKK = 0;
	for (int i = m_nCurPartIndex; i < nCount; i++)
	{
		ChromList[nKK] = gl.m_lstChromFiles.GetAt(i);
		nKK++;
	}
	for (int i = nKK; i < 10; i++)
		ChromList[i].strFileName = _T("");

	InvalidateRect(m_lstArea);
}

void CCalibrateView::OnBnClickedBtup()
{
	// TODO: 在此添加控件通知处理程序代码
	int nCount = gl.m_lstChromFiles.GetCount();
	m_nCurPartIndex = m_nCurPartIndex - 10;
	if (m_nCurPartIndex < 0) m_nCurPartIndex = 0;
	int nKK = 0;
	if (nCount > m_nCurPartIndex+10)
	{
		for (int i = m_nCurPartIndex; i < m_nCurPartIndex +10; i++)
		{
			ChromList[nKK] = gl.m_lstChromFiles.GetAt(i);
			nKK++;
		}
	}
	else
	{
		for (int i = m_nCurPartIndex; i < nCount; i++)
		{
			ChromList[nKK] = gl.m_lstChromFiles.GetAt(i);
			nKK++;
		}
		for (int i = nKK; i < 10; i++)
			ChromList[i].strFileName = _T("");
	}
	InvalidateRect(m_lstArea);
}


void CCalibrateView::OnBnClickedBtdown()
{
	// TODO: 在此添加控件通知处理程序代码
	int nCount = gl.m_lstChromFiles.GetCount();
	m_nCurPartIndex = m_nCurPartIndex + 10;
	if (m_nCurPartIndex > nCount) m_nCurPartIndex = nCount-10;
	if (m_nCurPartIndex < 0) m_nCurPartIndex = 0;
	int nKK = 0;
	if (nCount > m_nCurPartIndex + 10)
	{
		for (int i = m_nCurPartIndex; i < m_nCurPartIndex +10; i++)
		{
			ChromList[nKK] = gl.m_lstChromFiles.GetAt(i);
			nKK++;
		}
	}
	else
	{
		for (int i = m_nCurPartIndex; i < nCount; i++)
		{
			ChromList[nKK] = gl.m_lstChromFiles.GetAt(i);
			nKK++;
		}
		for (int i = nKK; i < 10; i++)
			ChromList[i].strFileName = _T("");
	}
	InvalidateRect(m_lstArea);
}

CString		CCalibrateView::GetTimeFromFileName(CString strFileName)
{
	if (strFileName.GetLength()==0) return _T("");
	int nIndex = strFileName.ReverseFind('\\');
	strFileName= strFileName.Right(strFileName.GetLength() - nIndex - 1);
	nIndex = strFileName.Find(_T("_"));
	strFileName = strFileName.Mid(nIndex + 6);
	strFileName = strFileName.Left(strFileName.GetLength() - 4);
	CString strTemp1, strTemp2, strTemp3;
	strTemp1 = strFileName.Left(2);
	strTemp3= strFileName.Mid(2);
	strTemp2= strTemp3.Left(2);
	strTemp3= strTemp3.Mid(2);
	strFileName.Format(_T("%s:%s:%s"), strTemp1, strTemp2, strTemp3);
	return strFileName;
}


void	CCalibrateView::DrawChromList(CDC* pDC, CRect rt)
{
	pDC->SetBkMode(TRANSPARENT);
	LOGFONT lf;								//LOGFONT结构中的变量用于定义字体的各种特性
	memset(&lf, 0, sizeof(lf));
	lf.lfCharSet = CHINESEBIG5_CHARSET;		//设置字符集
	lf.lfHeight = 12;
	wcscpy(lf.lfFaceName, _T("宋体"));		//设置字体名称 MingLiu为MS提供的BIG5字体
	CFont font;
	font.CreateFontIndirect(&lf);			//创建字体
	CFont* pF = (CFont*)pDC->SelectObject(&font);	//保存当前字体
	pDC->SetTextColor(RGB(64, 0, 64));

	float  dCellH = rt.Height() / 10.0;
	for (int i = 0; i < 10; i++)
	{
		CRect rtCell = rt;
		rtCell.top = rt.top + i*dCellH;
		rtCell.bottom = rt.top + (i + 1)*dCellH;
		m_rtFile[i] = rtCell;
		rtCell.DeflateRect(2, 2, 2, 2);
		pDC->FillSolidRect(rtCell,RGB(247,245,210));
		CString strTemp = GetTimeFromFileName(ChromList[i].strFileName);
		pDC->DrawText(strTemp, rtCell, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
	}
	pDC->SelectObject(pF);
}



void CCalibrateView::OnLButtonUp(UINT nFlags, CPoint point)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	for (int i = 0; i < 10; i++)
	{
		if (m_rtFile[i].PtInRect(point))
		{
			if (ChromList[i].strFileName.GetLength()>0)
			{
				Chrom_File cf = gl.ReadChmDate(ChromList[i].strFileName);
				m_wndIntResult.UpdateGrid(cf);
				int nType = GetChTypeFromFileName(ChromList[i].strFileName);
				m_wndCurve.UpdateGraph(cf, nType);
			}
		}
	}
	CFormView::OnLButtonUp(nFlags, point);
}


void CCalibrateView::OnBnClickedBtopenfile()
{
	// TODO: 在此添加控件通知处理程序代码
	CString strFile = _T("");
	CFileDialog dlg(true, TEXT("bcf"), NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, _T("色谱数据文件(*.bcf)|*.bcf||"));
	if (dlg.DoModal() != IDOK) return;
	strFile = dlg.GetPathName();
	if (strFile.GetLength()>0)
	{
		Chrom_File cf = gl.ReadChmDate(strFile);
		m_wndIntResult.UpdateGrid(cf);
		int nType = GetChTypeFromFileName(strFile);
		m_wndCurve.UpdateGraph(cf, nType);
	}
}


void CCalibrateView::OnTimer(UINT_PTR nIDEvent)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	if (m_nTimer == nIDEvent)
	{
		if(!m_bUpdateFormView)
		{
			CRect rect;
			GetClientRect(&rect);
			PostMessage(WM_SIZE, (WPARAM)SIZE_RESTORED, MAKELPARAM(rect.Width(), rect.Height()));
			m_bUpdateFormView=TRUE;
		}

		int nCount = gl.m_lstChromFiles.GetCount();
		if (nCount != m_nOldCount)
		{
			m_nOldCount = nCount;
			if (m_nCurPartIndex >= nCount - 11)
			{
				m_nCurPartIndex = nCount - 10;
				RefreshList();
				InvalidateRect(m_lstArea,FALSE);
			}
		}
	}
	CFormView::OnTimer(nIDEvent);
}


void CCalibrateView::OnBnClickedBtkeyboard()
{
	// TODO: 在此添加控件通知处理程序代码
	gl.StartNumberKeyBoard();
}


